﻿using DataWasher.WebApi.Repositories;
using Microsoft.Azure.Cosmos.Table;
using Report.WebApi.Interfaces;
using Report.WebApi.Models;
using Report.WebApi.Models.ResponseModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ToolManager.Entities;
using ToolManager.IService;

namespace Report.WebApi.Services
{

    /// <summary>
    /// 维修类别费用
    /// </summary>
    public class QueryWXLBFYService : QueryService, IQueryWXLBFYService
    {
        private readonly IStorageService _storageService;
        private readonly ISysDomainRepository _repository;
        public QueryWXLBFYService(IStorageService storageService, ISysDomainRepository repository) : base(repository)
        {
            _storageService = storageService;
            _repository = repository;
        }
        public async Task<List<WXLBFYResponse>> GetHomePageData(QueryRequestModel request)
        {
            var response = new List<WXLBFYResponse>();
            var thisData = new List<BusinessDataEntity>();

            try
            {
                //组装查询条件
                var thisYear = await GetMonthsThisYear(request);
                var filter = await ParamBuilder(request, thisYear);
                var query = new TableQuery<BusinessDataEntity>().Where(filter);
                var queryData = await _storageService.GetBusData("Businessdata", query);

                if (_connectionStrings.isDebug == 0)
                {
                    var domainIds = await GetDomainIds(request);
                    queryData = queryData.Where(x => domainIds.Contains(x.DomainId)).ToList();
                }

                if (request.QueryByYear || request.QueryHomePage)
                    thisData = queryData.Where(x => x.AddDateYM >= thisYear.QueryBegin && x.AddDateYM <= thisYear.QueryEnd).ToList();

                if (request.QueryByMonth || request.QueryByCustom || request.QueryByQuarter)
                    thisData = queryData.Where(x => x.AddDate >= thisYear.QueryBegin && x.AddDate <= thisYear.QueryEnd).ToList();

                if (request.QueryOrderByDesc)
                    response = thisData.GroupBy(x => x.MntCategory2).Select(n => new WXLBFYResponse { Category = n.Key, Count = n.Sum(m => decimal.Parse(m.Settlement) == 0 ? decimal.Parse(m.Cost) : decimal.Parse(m.Settlement)) }).OrderByDescending(x => x.Count).Take(5).ToList();
                else
                    response = thisData.GroupBy(x => x.MntCategory2).Select(n => new WXLBFYResponse { Category = n.Key, Count = n.Sum(m => decimal.Parse(m.Settlement) == 0 ? decimal.Parse(m.Cost) : decimal.Parse(m.Settlement)) }).OrderBy(x => x.Count).Take(5).ToList();
                return response;

            }
            catch (Exception ex)
            {

                throw ex;
            }

            return response;
        }

        public async override Task<string> ParamBuilder(QueryRequestModel request, YearMonthModel filterModel)
        {
            var partitionKey = DateTime.Now.ToString("yyyyMMdd");
            var filterSB = new StringBuilder();
            filterSB.Append($"(PartitionKey eq '{partitionKey}')");
            filterSB.Append(" and ");

            filterSB.Append($"(AddDateYear eq {filterModel.Year})");

            #region 条件组合
            filterSB.Append(" and ");
            filterSB.Append($"(Language eq {request.Language})");

            if (!string.IsNullOrEmpty(request.QueryStore))
            {
                filterSB.Append(" and ");
                filterSB.Append($"(TagName eq '{request.QueryStore}')");
            }

            if (!string.IsNullOrEmpty(request.QueryStatus))
            {
                filterSB.Append(" and ");
                filterSB.Append($"(MntStatus eq '{request.QueryStatus}')");
            }

            if (!string.IsNullOrEmpty(request.QueryPriority))
            {
                filterSB.Append(" and ");
                filterSB.Append($"(Priority eq '{request.QueryPriority}')");
            }
            if (!string.IsNullOrEmpty(request.QueryMntanCategory))
            {
                filterSB.Append(" and ");
                filterSB.Append($"(MntCategory2 eq '{request.QueryMntanCategory}')");
            }
            #endregion
            return filterSB.ToString();
        }

    }
}


